﻿define(['geomap/plugins/layersV4/ExGraphicsLayer', 'esri/PopupTemplate', 'esri/Graphic'], function (
    GraphicsLayer,
    PopupTemplate,
    Graphic
) {
    var PointLayer = GraphicsLayer.createSubclass({
        setData: function (viewUtils, layer) {
            var me = this;
            if (!viewUtils) {
                return;
            }
            me.viewUtils = viewUtils;
            var options = layer.layer || {};
            me.options = options;
            var data = options.data || [];
            me.screenSizePerspectiveEnabled = options.screenSizePerspectiveEnabled;
            var popupTemplateOptions = options.popupTemplateOptions;
            // var lngLatArray = mapUtils.toLngLatArray(viewUtils, data, options);
            var symbolFieldName = options.symbolFieldName || 'symbol';
            var lngFieldName = options.lngFieldName || 'x';
            var latFieldName = options.latFieldName || 'y';
            var sr = me.spatialReference; //options.sr;
            // me.sr=sr;
            me.popupOptions = Object.assign(
                {
                    visible: false,
                    isMultiPopup: false,
                    contentField: 'content',
                    isShowCloseIcon: false,
                },
                options.popupOptions
            );
            let symbol = {
                type: 'simple-marker',
                color: [226, 119, 40], // Orange
                outline: {
                    color: [255, 255, 255], // White
                    width: 1,
                },
            };
            data.map((item, index) => {
                const geometry = {
                    type: 'point',
                    longitude: null,
                    latitude: null,
                };
                if (lngFieldName) {
                    geometry.longitude = item[lngFieldName];
                    geometry.latitude = item[latFieldName];
                }
                if (item[symbolFieldName]) {
                    me.addGraphic(geometry, item[symbolFieldName], item, popupTemplateOptions, sr);
                    if (index == data.length - 1) {
                        if (typeof options.callback == 'function') {
                            options.callback(layer);
                        }
                    }
                } else {
                    me.addGraphic(geometry, symbol, item, popupTemplateOptions, sr);
                    if (index == data.length - 1) {
                        if (typeof options.callback == 'function') {
                            options.callback(layer);
                        }
                    }
                }
            });

            if (options.popupOptions && options.popupOptions.visible) {
                me.openCustomPopup(viewUtils, data);
            }
            me._showHighlight(viewUtils, options);
        },
        convertSr: function (viewUtils, lngLatArray, callback) {
            var me = this;
            var mapSr = viewUtils.getSpatialReference();
            me.mapSr = mapSr;
            var layerSr = me.spatialReference;
            /*坐标转换，扩展的div不像svg可以自动转换坐标，如果点位的坐标与底图的坐标不一致需要先手工转换坐标*/
            //by MHQ 以目标坐标系验证当前数据是否需要转换
            if (lngLatArray && lngLatArray.length && !me.isConvertSr(mapSr.wkid, lngLatArray[0])) {
                if ($.isFunction(callback)) {
                    callback(lngLatArray, mapSr);
                }
                return;
            }
            if (mapSr.wkid == '102100') {
                // if (layerSr.wkid == '4326' || layerSr.wkid == '4490') {
                me.lngLatToXY(viewUtils, lngLatArray, function (lngLatArray) {
                    if ($.isFunction(callback)) {
                        callback(lngLatArray, mapSr);
                    }
                });
                return;
                // };
            }
            // if (mapSr.wkid == '4326' || mapSr.wkid == '4490') {
            //     if (layerSr.wkid == '102100') {
            //         me.xyToLngLat(mapView, mapUtils, lngLatArray, function(lngLatArray) {
            //             if ($.isFunction(callback)) {
            //                 callback(lngLatArray, mapSr);
            //             };
            //         });
            //         return;
            //     };

            // };
            if ($.isFunction(callback)) {
                callback(lngLatArray, me.spatialReference);
            }
        },
        isConvertSr(targetWkid, obj) {
            let x = obj.x + '.';
            let len = x.indexOf('.');
            if (targetWkid == '102100') {
                if (len > 3) {
                    return false;
                } else {
                    return true;
                }
            } else if (targetWkid == '102100') {
                if (len > 3) {
                    return true;
                } else {
                    return false;
                }
            }
        },
        addGraphic: function (geometry, symbol, item, popupTemplateOptions, sr) {
            var me = this;

            var popupTemplate = null;
            if (popupTemplateOptions) {
                popupTemplate = new PopupTemplate();
                popupTemplate.title = popupTemplateOptions.getTitle(item);
                popupTemplate.content = popupTemplateOptions.getContent(item);
            }

            var graphic = this.createGraphic({
                geometry: geometry,
                symbol: symbol,
                attributes: item,
                popupTemplate: popupTemplate,
            });
            me.add(graphic);
        },
        getGraphicLocation: function (viewUtils, esriEvent) {
            var me = this;
            if (!esriEvent || !esriEvent.graphic) {
                return;
            }
            if (!viewUtils) {
                return;
            }

            var map = viewUtils.getMap();
            if (!map) {
                return;
            }

            var geometry = esriEvent.graphic.geometry || {};
            // var xoffset = esriEvent.graphic.symbol.xoffset;
            // var yoffset = esriEvent.graphic.symbol.yoffset;
            // var screenPoint = map.toScreen(geometry);
            // screenPoint.x += xoffset;
            // screenPoint.y -= yoffset;
            // geometry = map.toMap(screenPoint);
            var lngFieldName = 'x';
            var latFieldName = 'y';
            var location = {};
            location[lngFieldName] = geometry.longitude;
            location[latFieldName] = geometry.latitude;
            return location;
        },
        createGraphic(t) {
            t = Object.assign(
                { geometry: null, symbol: null, attributes: null, popupTemplate: null },
                t
            );
            let g = new Graphic(t);
            return g;
        },
    });
    return PointLayer;
});
